//-----------------------------------------------------------------------
// <copyright file="UserDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using APPro.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace APPro.Data
{    
    public abstract class UserDAOBase
    {
        #region Common methods
        public virtual User CreateUserFromReader(IDataReader reader)
        {
            User item = new User();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("Mobile"))) item.Mobile = (string)reader["Mobile"];
                if (!reader.IsDBNull(reader.GetOrdinal("Password"))) item.Password = (string)reader["Password"];
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) item.Email = (string)reader["Email"];
                if (!reader.IsDBNull(reader.GetOrdinal("Ten"))) item.Ten = (string)reader["Ten"];
                if (!reader.IsDBNull(reader.GetOrdinal("Ho"))) item.Ho = (string)reader["Ho"];
                if (!reader.IsDBNull(reader.GetOrdinal("NgSinh"))) item.NgSinh = (DateTime)reader["NgSinh"];
                if (!reader.IsDBNull(reader.GetOrdinal("Phai"))) item.Phai = (string)reader["Phai"];
                if (!reader.IsDBNull(reader.GetOrdinal("DChi"))) item.DChi = (string)reader["DChi"];
                if (!reader.IsDBNull(reader.GetOrdinal("NhanMail"))) item.NhanMail = (bool)reader["NhanMail"];
                if (!reader.IsDBNull(reader.GetOrdinal("NhanSMS"))) item.NhanSMS = (bool)reader["NhanSMS"];
                if (!reader.IsDBNull(reader.GetOrdinal("ThanhPho"))) item.ThanhPho = (int)reader["ThanhPho"];
                if (!reader.IsDBNull(reader.GetOrdinal("Quan"))) item.Quan = (int)reader["Quan"];
                if (!reader.IsDBNull(reader.GetOrdinal("NgonNgu"))) item.NgonNgu = (int)reader["NgonNgu"];
                if (!reader.IsDBNull(reader.GetOrdinal("NguoiGioiThieu"))) item.NguoiGioiThieu = (string)reader["NguoiGioiThieu"];
                if (!reader.IsDBNull(reader.GetOrdinal("MaSoCaNhan"))) item.MaSoCaNhan = (string)reader["MaSoCaNhan"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateUserFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateUser methods
            
        public virtual void CreateUser(User user)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUsersInsert");
                
				database.AddInParameter(dbCommand, "@Mobile", DbType.String, user.Mobile);
				database.AddInParameter(dbCommand, "@Password", DbType.String, user.Password);
				database.AddInParameter(dbCommand, "@Email", DbType.StringFixedLength, user.Email);
				database.AddInParameter(dbCommand, "@Ten", DbType.String, user.Ten);
				database.AddInParameter(dbCommand, "@Ho", DbType.String, user.Ho);
				database.AddInParameter(dbCommand, "@NgSinh", DbType.DateTime, user.NgSinh);
				database.AddInParameter(dbCommand, "@Phai", DbType.String, user.Phai);
				database.AddInParameter(dbCommand, "@DChi", DbType.String, user.DChi);
				database.AddInParameter(dbCommand, "@NhanMail", DbType.Boolean, user.NhanMail);
				database.AddInParameter(dbCommand, "@NhanSMS", DbType.Boolean, user.NhanSMS);
				database.AddInParameter(dbCommand, "@ThanhPho", DbType.Int32, user.ThanhPho);
				database.AddInParameter(dbCommand, "@Quan", DbType.Int32, user.Quan);
				database.AddInParameter(dbCommand, "@NgonNgu", DbType.Int32, user.NgonNgu);
				database.AddInParameter(dbCommand, "@NguoiGioiThieu", DbType.String, user.NguoiGioiThieu);
				database.AddInParameter(dbCommand, "@MaSoCaNhan", DbType.String, user.MaSoCaNhan);
				
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateUserException, ex);
            }
        }

        #endregion
		
        #region UpdateUser methods
        
        public virtual void UpdateUser(User user)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUsersUpdate");            
                
                database.AddInParameter(dbCommand, "@Mobile", DbType.String, user.Mobile);
                database.AddInParameter(dbCommand, "@Password", DbType.String, user.Password);
                database.AddInParameter(dbCommand, "@Email", DbType.StringFixedLength, user.Email);
                database.AddInParameter(dbCommand, "@Ten", DbType.String, user.Ten);
                database.AddInParameter(dbCommand, "@Ho", DbType.String, user.Ho);
                database.AddInParameter(dbCommand, "@NgSinh", DbType.DateTime, user.NgSinh);
                database.AddInParameter(dbCommand, "@Phai", DbType.String, user.Phai);
                database.AddInParameter(dbCommand, "@DChi", DbType.String, user.DChi);
                database.AddInParameter(dbCommand, "@NhanMail", DbType.Boolean, user.NhanMail);
                database.AddInParameter(dbCommand, "@NhanSMS", DbType.Boolean, user.NhanSMS);
                database.AddInParameter(dbCommand, "@ThanhPho", DbType.Int32, user.ThanhPho);
                database.AddInParameter(dbCommand, "@Quan", DbType.Int32, user.Quan);
                database.AddInParameter(dbCommand, "@NgonNgu", DbType.Int32, user.NgonNgu);
                database.AddInParameter(dbCommand, "@NguoiGioiThieu", DbType.String, user.NguoiGioiThieu);
                database.AddInParameter(dbCommand, "@MaSoCaNhan", DbType.String, user.MaSoCaNhan);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateUserException, ex);
            }
        }
        
        #endregion
		
        #region DeleteUser methods
        public virtual void DeleteUser(string mobile)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUsersDelete");
                
                database.AddInParameter(dbCommand, "@Mobile", DbType.String, mobile);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteUserException, ex);
            }
        }

        #endregion
		
        #region GetUser methods
        
        public virtual User GetUser(string mobile)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUsersGet");
                
                database.AddInParameter(dbCommand, "@Mobile", DbType.String, mobile);
                
                User user =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        user = CreateUserFromReader(reader);
                        reader.Close();
                    }
                }
                return user;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetUserException, ex);
            }
        }
        
        #endregion
		
        #region GetUserList methods
        public virtual UserCollection GetUserList(UserColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUsersGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                UserCollection userCollection = new UserCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        User user = CreateUserFromReader(reader);
                        userCollection.Add(user);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return userCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetUserListException, ex);
            }
        }
        
        public virtual UserCollection GetUserList(UserColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetUserList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}